package com.chapter.sample.common.component;

import com.chapter.sample.common.Constant;
import com.chapter.sample.common.config.HostConfig;
import com.chapter.sample.common.enums.HostTypeEnum;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;



@Component
@Slf4j
public class ApplicationFactory implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Autowired
    private HostConfig hostConfig;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (ApplicationFactory.applicationContext == null) {
            ApplicationFactory.applicationContext = applicationContext;
        }
    }
    public HostTypeEnum getHostType(String type) {
        return hostConfig.getType(type);
    }

    public HostTypeEnum getHostType(HttpServletRequest request) {
        String header = request.getHeader(Constant.REQUEST_HOST);
        log.info("REQUEST_HOST:{}",header);
        return getHostType(header);
    }

}
